package com.example.database.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.RetryPolicy;
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;

import java.util.Collections;

/**
 * @author 梅芙雨Vanessa
 */
@Configuration
public class RetryConfig {
    @Bean
    public RetryTemplate retryTemplate() {
        RetryTemplate retryTemplate = new RetryTemplate();
        // 最大重试尝试次数
        RetryPolicy retryPolicy = new SimpleRetryPolicy(5, Collections.singletonMap(RuntimeException.class, true));
        retryTemplate.setRetryPolicy(retryPolicy);
        // 退避策略（指数退避）
        ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
        // 初始重试间隔（毫秒）
        backOffPolicy.setInitialInterval(1000);
        // 重试间隔的乘数
        backOffPolicy.setMultiplier(2.0);
        retryTemplate.setBackOffPolicy(backOffPolicy);
        return retryTemplate;
    }
}
